package com.milo.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;

/***
* @description: Url地址编码/解码工具类
* @author: Milogenius
* @date: 2018/8/28 9:52 
**/
public class UrlUtils {
	
	/**
	 * encode或decode字符串处理，返回编码的字符串
	 * @param str
	 */
	public static String retEncodeStr(String str){
		try {
			if(StringUtils.isNotEmpty(str)){
				String decodeStr = URLDecoder.decode(str,"utf-8");
				if(str.equals(decodeStr)){
					return URLEncoder.encode(decodeStr,"utf-8");
				}else{
					return str;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * encode或decode字符串处理，返回正常的字符串
	 * @param str
	 */
	public static String retDecodeStr(String str){
		try {
			if(StringUtils.isNotEmpty(str)){
				String decodeStr = URLDecoder.decode(str,"utf-8");
				if(str.equals(decodeStr)){
					return str;
				}else{
					return decodeStr;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void main(String[] args) {
		String payResultContent = "%3CAdvPay%3E%3CPubInfo%3E%3CVersion%3E1.00%3C%2FVersion%3E%3CTransactionId%3E93120180712190238282190%3C%2FTransactionId%3E%3CTransactionDate%3E20180712190531%3C%2FTransactionDate%3E%3COriginId%3E0012%3C%2FOriginId%3E%3CVerifyCode%3E8691a3551f0227f5999f66b93cabd297%3C%2FVerifyCode%3E%3CDigestAlg%3EMD5%3C%2FDigestAlg%3E%3C%2FPubInfo%3E%3CBusiData%3E%3CStatus%3E0%3C%2FStatus%3E%3COrderId%3E93120180712190238282190%3C%2FOrderId%3E%3CDoneCode%3ETP201807121906010941010012%3C%2FDoneCode%3E%3CPayAmount%3E0.01%3C%2FPayAmount%3E%3CPayDate%3E20180712190726%3C%2FPayDate%3E%3CType%3Ewechatpay%3C%2FType%3E%3CVirtualCurrency%3E0%3C%2FVirtualCurrency%3E%3CthirdAmount%3E0.01%3C%2FthirdAmount%3E%3C%2FBusiData%3E%3C%2FAdvPay%3E";
		String payResultContent1 = "http://123.com/?abcs=123";
		System.out.println(retDecodeStr(payResultContent));
		System.out.println(retDecodeStr(payResultContent1));
		System.out.println(retEncodeStr(payResultContent));
		System.out.println(retEncodeStr(payResultContent1));
	}

}
